#!/usr/bin/env python
# Create incident in ServiceNow for Host

import argparse, ConfigParser, logging, os
from SOAPpy import SOAPProxy

logging.basicConfig(filename='/opt/omd/sites/labf/var/log/snow_ticketer.log', format='%(asctime)s %(levelname)s: %(message)s', level=logging.DEBUG)
logging.debug(os.environ)
logging.debug(os.environ['NOTIFY_HOSTNAME'])

username = 'bmcuser'
password = 'bmcpass'
instance = 'webhost'

proxy = 'https://%s:%s@%s/MRcgi/MRWebServices.pl' % (username, password, instance)
namespace = 'MRWebServices'
action = 'MRWebServices#MRWebServices__getIssueDetails'

def createincident(params_dict):
	server = SOAPProxy(proxy, namespace)
	
	try:
		response = server.insert(cmdb_ci=(params_dict['host']), impact=(params_dict['impact']), assignment_group=(params_dict['group']), short_description=(params_dict['shortdesc']), description=(params_dict['desc']))
		return (response)
	except:
		print ('Error, error')
		logging.error('Error, error')
		exit (1)

def main():
	host = os.environ['NOTIFY_HOSTNAME']
	state = os.environ['NOTIFY_HOSTSTATE']
	impact = 1
	group = os.environ['NOTIFY_CONTACTALIAS']
	longdatetime = os.environ['NOTIFY_LONGDATETIME']
	hostoutput = os.environ['NOTIFY_HOSTOUTPUT']
	shortdesc = host + " is " + state
	desc = "Host: " + host + "\nState: " + state + "\nTime: " + longdatetime + "\nDescription: " + hostoutput + "\n" 
	
	options = {'host': host, 'state': state, 'impact': impact, 'group': group, 'shortdesc': shortdesc, 'desc': desc}
	print(options)
	logging.debug(options)
	
	new_incident_sysid=createincident(options)
	print(new_incident_sysid)

	incident, status, action = 'INC007', 'created', 'create' 
	logging.info('Incident:%s Status:%s Action:%s', incident, status, action)

if __name__ == '__main__':
	main()
